
//
//  NSMAsset.m
//  NSMKit
//
//  Created by nsm on 2019/3/5.
//  Copyright © 2019 nani. All rights reserved.
//

#import "NSMAsset.h"

@interface NSMAsset ()
@property(nonatomic, strong) PHAsset *asset;
@end

@implementation NSMAsset
+ (instancetype)assetWithPHAsset:(PHAsset *)asset {
    return [[self alloc] initWithPHAsset:asset];
}

- (instancetype)initWithPHAsset:(PHAsset *)asset {
    self = [super init];
    if (self) {
        _asset = asset;
    }
    return self;
}

@end

@interface NSMAssetsGroup ()
@property (nonatomic, strong) PHAssetCollection *assetCollection;
@property (nonatomic, strong) PHFetchResult *fetchResult;
@property (nonatomic, strong) NSMutableArray<NSMAsset *> *assets;
@end

@implementation NSMAssetsGroup
+ (instancetype)groupWithAssetCollection:(PHAssetCollection *)assetCollection options:(PHFetchOptions *)options {
    return [[self alloc] initWithAssetCollection:assetCollection options:options];
}

- (instancetype)initWithAssetCollection:(PHAssetCollection *)assetCollection options:(PHFetchOptions *)options {
    self = [super init];
    if (self) {
        _assetCollection = assetCollection;
        _fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
        
        _assets = @[].mutableCopy;
        for (PHAsset *asset in _fetchResult) {
            [_assets addObject:[NSMAsset assetWithPHAsset:asset]];
        }
    }
    return self;
}

- (NSUInteger)numberOfAssets {
    return _fetchResult.count;
}

@end
